/* api */
import { getTenantImSetting } from '@src/api/ImApi'
/* entity */
import { useFetchResult } from '@hooks/useFetch'
/* hooks */
import IMSetting from '@model/entity/IMSetting'
/* model */
import { GetTenantImSettingModel } from '@model/param/in/IM'
/* vue */
import { computed, ComputedRef } from 'vue'

type UseIMSettingReturnType = [ComputedRef<IMSetting>, ComputedRef<boolean>]

export function useIMSetting(): UseIMSettingReturnType {
  const IMSettingData: ComputedRef<IMSetting> = useFetchIMSetting()
  
  const IMEnabled = computed(() => Boolean(IMSettingData.value?.enabled))
  
  return [IMSettingData, IMEnabled]
}

export function useFetchIMSetting(): ComputedRef<IMSetting> {
  
  let [data, callback] = useFetchResult<GetTenantImSettingModel, IMSetting>(async () => {
    return getTenantImSetting()
  }, {})
  
  callback()
  
  return computed(() => data.value as IMSetting)
}
